# -*- coding:utf-8 -*-

import Pluto,struct,codecs,os,fnmatch

fl=Pluto.walk('msg')
for fn in fl:
        if not fnmatch.fnmatch(fn,'*.msb'):
                continue
        print fn
        fp=open(fn,'rb')
        num=struct.unpack('i',fp.read(4))[0]
        tar=codecs.open(fn+'.txt','wb','utf16')
        for i in xrange(num):
                fp.seek(4+i*8)
                offset=struct.unpack('i',fp.read(4))[0]+4
                size=struct.unpack('i',fp.read(4))[0]
                fp.seek(offset)
                string=[]
                while True:
                        newoffset=fp.tell()
                        if newoffset==offset+size:
                                break
                        byte=fp.read(1)
                        if ord(byte)<0x80:
                                string.append(byte)
                        else:
                                fp.seek(newoffset)
                                word=fp.read(2).decode('sjis')
                                string.append(word)
                print ''.join(string)
                tar.write('No.%d'%i+'\r\n'+u'－'*18+'\r\n'+''.join(string)+'\r\n'+u'－'*18+'\r\n'*2)
        
        fp.close()
        tar.close()